package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** /eduservice/edu-teacher/pageTeacherCondition/{current}/{limit}
 * @author testjava
 * @since 2021-03-03
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
//@CrossOrigin  //解决跨域
public class EduTeacherController {

    //1.把service注入
    @Autowired
    private EduTeacherService eduTeacherService;
    //查询所有讲师数据库数据
    //rest  get提交
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R finfAllTeacher(){

        try {
            int i=10;
        } catch (Exception e) {
            throw new GuliException(20001,"出现自定义异常");
        }
        //调用service
        List<EduTeacher> teachers = eduTeacherService.list(null);
        return R.ok().data("items",teachers);

    }

    /**
     *   根据ID删除讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true)
                                  @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }

    }



    /**
     *     分页Controller方法
     * @param current 当前页
     * @param limit   每页记录数
     * @return
     */
    @ApiOperation(value = "分页显示讲师列表")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageList(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        //分页对象
        Page<EduTeacher> pageParam = new Page<>(current, limit);
        //分页方法
        eduTeacherService.page(pageParam, null);

        List<EduTeacher> records = pageParam.getRecords(); //数据list集合

        long total = pageParam.getTotal();  //总记录数

//        Map map=new HashMap();
//        map.put("total", total);
//        map.put("rows", records);
//        return R.ok().data(map);

        return R.ok().data("total", total).data("rows", records);
    }

    /*
        条件分页查询
     */
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition( @ApiParam(name = "current", value = "当前页码", required = true)
                                       @PathVariable Long current,
                                   @ApiParam(name = "limit", value = "每页记录数", required = true)
                                       @PathVariable Long limit,
                                   @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                                       @RequestBody(required = false)  TeacherQuery teacherQuery){//@RequestBody  post提交

        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();


        if(!StringUtils.isEmpty(name)){
            //构建名字条件
            wrapper.like("name",name);//表中字段名称
        }
        if (!StringUtils.isEmpty(level) ) {

            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {

            wrapper.ge("gmt_create", begin);//ge大于等于
        }
        if (!StringUtils.isEmpty(end)) {

            wrapper.le("gmt_create", end);//le小于等于
        }


        //排序
        wrapper.orderByDesc("gmt_create");

        eduTeacherService.page(pageTeacher,wrapper);

        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
        long total = pageTeacher.getTotal();  //总记录数


        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 添加讲师的接口
     * @param teacher
     * @return
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){

        boolean save = eduTeacherService.save(teacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 根据ID查询讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/getTeacher/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("/updateById")
    public R updateById(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){
        boolean update = eduTeacherService.updateById(teacher);
        if(update){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

